<template>
  <div class="login_container">
    <!-- 头像区域 -->
    <div class="avatar_box">
      <img src="../assets/logo.png" alt />
    </div>
    <div class="login_box">
      <!-- 登录表单区域 -->
      <!-- 表单验证也是element-ui -->

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 学生
        <el-select v-model="loginForm.value" placeholder="学生">
          <el-option
            v-for="item in loginForm.usertype"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>-->
        <el-form-item class="title">
          <p>主观题智能阅卷系统</p>
        </el-form-item>

        <!-- <el-form-item> -->
        <!-- 学生 -->
        <!-- <el-select v-model="loginForm.value" placeholder="学生">
            <el-option
              v-for="item in loginForm.usertype"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>-->

        <!-- 账户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="账户名"></el-input>
        </el-form-item>
        <!-- 账户密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="账户密码"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="danger" @click="login" round>登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { log } from 'util'
export default {
  data() {
    return {
      loginForm: {
        // usertype: [
        //   {
        //     value: '1',
        //     label: '学生'
        //   },
        //   {
        //     value: '2',
        //     label: '教师'
        //   },
        //   {
        //     value: '3',
        //     label: '系统管理员'
        //   }
        // ],
        username: '',
        password: '',
        type: []
      },
      loginFormRules: {
        username: [
          // required:true 说明该处必填项；不符合规则提示该信息；触发条件，失去焦点
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },

    // 网络
    login() {
      // async,await是为简化promise的返回值
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.get('user/login', {
          params: {
            userName: this.loginForm.username,
            password: this.loginForm.password
          }
        })
        console.log(res)
        if (res.code !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')

        // 保存用户id
        // window.sessionStorage.setItem('userId', res.data.id)
        // 这一步在store已经进行

        this.$store.commit('changeLogin', { userId: res.data.id })

        this.getRole(this.$store.state.userId)
      })
    },

    // 权限
    async getRole(id) {
      const { data: res } = await this.$http.get('user/getRoleList/' + id)
      if (res.code !== 200) return this.$message.error('获取权限失败')
      this.loginForm.type = res.data

      // 跳转页面
      // 选择跳转的不同home

      if (this.loginForm.type[0] == '学生') {
        this.$router.push('/stuhome')
      } else if (this.loginForm.type[0] == '教师') {
        this.$router.push('/teahome')
      } else if (this.loginForm.type[0] == '系统管理员') {
        this.$router.push('/adminhome')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: white;
  height: 100%;
  // background-image: url('../assets/psb (8).jpg');
  // background-size: 1536px 754.4px;
  .avatar_box {
    margin-top: 100px;
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  // .avatar_box {
  //   height: 130px;
  //   width: 130px;
  //   border: 1px solid #eee;
  //   border-radius: 50%;
  //   padding: 10px;
  //   box-shadow: 0 0 10px #ddd;
  //   position: absolute;
  //   left: 50%;
  //   transform: translate(-50%, -50%);
  //   background-color: #fff;
  //   img {
  //     width: 100%;
  //     height: 100%;
  //     border-radius: 50%;
  //     background-color: #eee;
  //   }
  // }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  .title {
    text-align: center;
    p {
      font-size: 25px;
    }
  }
  .el-select {
    // margin-bottom: 20px;
    width: 410px;
  }
}

.btns {
  // display: flex;
  // justify-content: flex-end;
  .el-button {
    width: 100%;
  }
}
</style>